package com.minelx.datamicroscope.querylanguage.common;

import java.util.*;

import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableMap;

public class ComboMap<K, V> {

	private final Map<K, List<V>> mapping;

	public ComboMap() {
		mapping = new HashMap<>();
	}

	public void add(K key, V value) {
		List<V> values = obtainValues(key);
		values.add(value);
		mapping.put(key, values);
	}

	public boolean containsKey(K key) {
		return mapping.containsKey(key);
	}

	public List<V> get(K key) {
		return unmodifiableList(obtainValues(key));
	}

	public Map<K, List<V>> toMap() {
		return unmodifiableMap(mapping);
	}

	public Collection<List<V>> values() {
		return mapping.values();
	}

	private List<V> obtainValues(K key) {
		if (!mapping.containsKey(key)) {
			return new LinkedList<>();
		}
		return mapping.get(key);
	}
}
